<template>
	<div class="zh-examinationPaper-relevance">
		<div class="zh-table">
			<div class="zh-table-open clear">
				<div class="zh-open-left fl">
					<span>学段：</span>
					<div class="open-width" style="width: 120px">
						<el-select v-model="listParam.study_section_id" filterable placeholder="请选择">
						    <el-option
						      v-for="item in orgLevel2"
						      :key="item.id"
						      :label="item.name"
						      :value="item.id">
						    </el-option>
						</el-select>
					</div>
					<span>学科：</span>
					<div class="open-width" style="width: 120px">
						<el-select v-model="listParam.subject_id" filterable placeholder="请选择">
						    <el-option
						      v-for="item in orgSubject"
						      :key="item.id"
						      :label="item.name"
						      :value="item.id">
						    </el-option>
						</el-select>
					</div>
				</div>
				<div class="zh-open-right fr">
					<el-button type="primary" @click="saveRelation">保存</el-button>
					<el-button type="primary" @click="refresh">刷新</el-button>
				</div>
			</div>
			<div class="zh-checkbox-list"
				v-loading="loading.status"
				:element-loading-text="loading.txt"
			>
				<el-checkbox-group v-model="checked">
				<ul>
					<li v-for="(item,key) in listData">
						<el-checkbox :label="parseInt(item.id)">{{ item.name }}</el-checkbox>
					</li>
				</ul>
				</el-checkbox-group>
			</div>
		</div>
	</div>
</template>
<script>
	export default {
		data(){
			return {
				loading:{
					status:false,
					txt:'获取数据...',
				},
				listParam:{
		    		study_section_id:'',
		    		subject_id:'',
		        },
				orgLevelParam:{ //获取学段,年纪参数
					level:2,
					pid:1,
					direction:'balance',
				},
				orgLevel2:[],
				subjectParam:{ //获取科目,版本，参数
					type:2,
					org_id:4,
					status:1,
				},
				checked: [],
				listData:[],
				orgSubject:[],
			}
		},
		created(){
			this.getOrgLevel2();
			// this.getSubject();

			this.getList();
		},
		watch:{
			
			'listParam.study_section_id':{
	    		handler(newVal,oldVal){
					this.getSubject();
　　　　　　　　}
			},
			'listParam.subject_id':{
	    		handler(newVal,oldVal){
					this.getRelation();
　　　　　　　　}
			}
		},
		methods: {
			refresh(){
				this.getList();
				this.listParam = {
					study_section_id :'',
					subject_id:''
				}
			},
			saveRelation(){
				if(this.listParam.study_section_id == '' ){
					this.$message.warning('请选择学段')
					return false;
				}
				if(this.listParam.subject_id == '' ){
					this.$message.warning('请选择学科')
					return false;
				}
				if(this.checked.length === 0 ){
					this.$message.warning('请选择关联题型')
					return false;
				}
				let success = ( data => {
		    		this.$message.success(data.msg)
					this.getList();
				});
				var param = {
					study_section_id:this.listParam.study_section_id,
					subject_id:this.listParam.subject_id,
					question_type_id:this.checked
				}
				this.$ajax.post('api/test/question_type_relation',param,{func:{success:success}})
				
			},
			getList(){
				this.loading.status = true;
            	let success = (data=>{
            		this.loading.status = false;
            		this.listData = data.data;
            	})
            	let fail = ( data => {
            		this.loading.status = false;
            		this.$message.error(data.msg)
            	})
            	//执行请求，并将回调方法变成config成员以便鉴权过期可以执行
            	this.$ajax.get('api/test/get_question_type',{params: {},func:{success:success,fail:fail}})
            },
            getRelation(){
            	this.loading.status = true;
            	let success = (data=>{
            		this.loading.status = false;
            		this.checked  = [];
            		if( data.data.length > 0){
            			for (var i = data.data.length - 1; i >= 0; i--) {
            				this.checked.push(parseInt(data.data[i].question_type_id));
            			}
            			
            		}
            	})
            	let fail = ( data => {
            		this.loading.status = false;
            		this.$message.error(data.msg)
            	})
            	//执行请求，并将回调方法变成config成员以便鉴权过期可以执行
            	this.$ajax.get('api/test/question_type_relation',{params: this.listParam,func:{success:success,fail:fail}})
            },	
			//获取学段
			getOrgLevel2(){
				let success = ( data=>{
            		this.orgLevel2 = data.data;
            		// this.orgLevel2.unshift({id:0,name:'全部'})
            	})
            	this.$ajax.get('api/org/org',{params: this.orgLevelParam,func:{success:success}})
			},
			
			//获取科目
			getSubject(){
				this.subjectParam.org_id = this.listParam.study_section_id;
				this.listParam.subject_id = '';
				let success = ( data=>{
            		this.orgSubject = data.data;
            		// this.orgSubject.unshift({id:0,name:'全部'});
            	})
            	this.$ajax.get('api/cate/subject',{params: this.subjectParam,func:{success:success}})
			},
			
			
		},
	}
</script>
<style lang="scss">
	.zh-examinationPaper-relevance {
		.zh-checkbox-list {
			margin: 26px 0;
			border: 1px solid #e6e6e6;
			ul {
				padding: 12px 0;
				li {
					padding-left: 20px;
					height: 40px;
					line-height: 40px;
					&:hover {
						background: #f5f7fa;
					}
				}
			}
		}
	}
</style>